package com.ideaaedi.springcloud.jd.user.controller.sys;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.ideaaedi.springcloud.jd.commonds.entity.PageInfo;
import com.ideaaedi.springcloud.jd.commonds.entity.Result;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysDictAddReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysDictListReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysDictTreeReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysDictUpdateReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysDictDetailRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysDictListRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysDictTreeRespVO;
import com.ideaaedi.springcloud.jd.user.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 系统 - 数据字典
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 *@since 2021.0.1.A
 */
@Slf4j
@Validated
@RestController
@ApiSort(value = 3)
@RequestMapping("/sys-dict")
@Api(value = "系统-数据字典", tags = {"系统-数据字典"})
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    /**
     * 增
     *
     * @param req 参数
     *
     * @return 新增的数据详情
     */
    @PostMapping("/add")
    @ApiOperation(value = "增")
    @ApiOperationSupport(order = 1)
    public Result<SysDictDetailRespVO> add(@RequestBody @Validated SysDictAddReqVO req) {
        return Result.success(sysDictService.add(req));
    }

    /**
     * 删
     *
     * @param id 要删除数据的id
     *
     * @return 删除了的数据详情
     */
    @ApiOperation(value = "删")
    @DeleteMapping("/delete/{id}")
    @ApiOperationSupport(order = 2)
    public Result<SysDictDetailRespVO> delete(@ApiParam(value = "要删除数据的id", required = true)
                                              @PathVariable("id") @NotNull(message = "id cannot be null.") Integer id) {
        return Result.success(sysDictService.delete(id));
    }

    /**
     * 改
     *
     * @param req
     *         参数
     *
     * @return 修改后的数据详情
     */
    @PutMapping("/update")
    @ApiOperation(value = "改")
    @ApiOperationSupport(order = 3)
    public Result<SysDictDetailRespVO> update(@RequestBody @Validated SysDictUpdateReqVO req) {
        return Result.success(sysDictService.update(req));
    }

    /**
     * 查列表
     *
     * @param req
     *         参数
     *
     * @return 数据列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查列表")
    @ApiOperationSupport(order = 4)
    public Result<PageInfo<SysDictListRespVO>> list(@RequestBody @Validated SysDictListReqVO req) {
        return Result.success(sysDictService.list(req));
    }

    /**
     * 查字典树
     *
     * @param req
     *         参数
     *
     * @return 数据树(k - 字典类型code; v - (k - 字典code, v - 字典数据详情))
     */
    @PostMapping("/tree")
    @ApiOperation(value = "查字典树")
    @ApiOperationSupport(order = 5)
    public Result<Map<String, Map<String, SysDictTreeRespVO>>> tree(@RequestBody @Validated SysDictTreeReqVO req) {
        return Result.success(sysDictService.tree(req));
    }
    
    /**
     * 查详情
     *
     * @param id 要查询数据的id
     *
     * @return 数据详情
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "查详情")
    @ApiOperationSupport(order = 6)
    public Result<SysDictDetailRespVO> detail(@ApiParam(value = "要查询数据的id", required = true)
                                              @PathVariable("id") @NotNull(message = "id cannot be null.") Integer id) {
        return Result.success(sysDictService.detail(id));
    }
}
